package com.cherrish.demo;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author cherrish
 * @time 2018-12-18 10:24
 * @name TestNewInstance
 * @desc:
 */
public class TestNewInstance {
    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
        test1();
//        test2();
//        test3();
        test4();
    }
    private static void test1(){
        Employee employee = new Employee("001", "LYX");
        System.out.println(employee);
    }

    private static void test2() throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //须含有无参构造
        Employee employee = (Employee) Class.forName("com.cherrish.demo.Employee").newInstance();
        System.out.println(employee);

        Employee employee1 = Employee.class.newInstance();
        System.out.println(employee1);
    }

    private static void test3() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        //须含有无参构造 或指定参数
        Constructor<Employee> constructor = Employee.class.getConstructor();
        Employee employee = constructor.newInstance();
        System.out.println(employee);
    }

    private static void test4() throws IllegalAccessException, InstantiationException {
        //要使用clone方法，我们需要先实现Cloneable接口并实现其定义的clone方法。
        //Employee employee = (Employee) emp3.clone();
    }

    private static void test5() {
        //反序列化
    }

}
